﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;
using School.Common;

namespace School.Service.Clothes
{
    public class SRV_CLothe
    {
        private static SchoolEntities db = new SchoolEntities();
        public static IEnumerable<SRV_Clothe> AllClothes()
        {
            var query = from Clothe in db.SRV_Clothe select Clothe;
            return query.ToList();
        }
        public static IEnumerable<SRV_Clothe> GetById(int id)
        {
            var query = from Clothe in db.SRV_Clothe
                        where Clothe.Id == id
                        select Clothe;
            return query.ToList();
        }
        public static bool ClothesInsert(SRV_Clothe Clothe, out long clotheId)
        {

            if (Clothe.Grad_Id == 0)
                Clothe.Grad_Id = null;
            if (Clothe.Cer_Id == 0)
                Clothe.Cer_Id = null;
            if (Clothe.Stage_Id == 0)
                Clothe.Stage_Id = null;
            db.AddToSRV_Clothe(Clothe);
            try
            {
                db.SaveChanges();
                clotheId = Clothe.Id;

                return true;
            }
            catch
            {
                clotheId = 0;
                return false;
            }
        }
        public static bool ClothesUpdate(SRV_Clothe ClotheToUpdate)
        {
            db.SRV_Clothe.Where(s => s.Id == ClotheToUpdate.Id).FirstOrDefault().Code = ClotheToUpdate.Code;

            var query = (from cloth in db.SRV_Clothe
                         where cloth.Id == ClotheToUpdate.Id
                         select cloth).Single();
            query.Name = ClotheToUpdate.Name;
            query.Description = ClotheToUpdate.Description;
            query.Cer_Id = ClotheToUpdate.Cer_Id;
            query.Campus_Id = ClotheToUpdate.Campus_Id;
            query.Grad_Id = ClotheToUpdate.Grad_Id;
            query.Stage_Id = ClotheToUpdate.Stage_Id;
            //query.Code = ClotheToUpdate.Code;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool ClotheDelete(long id)
        {
            var query = (from clothtype in db.SRV_Clothe
                         where clothtype.Id == id
                         select clothtype).Single();
            if (School.Service.Clothes.SRV_Clothe_Sizes_Size.ClothesSizeDelete(School.Service.Clothes.SRV_Clothe_Sizes_Size.AllClothesSizes().Where(s => s.Clothe_Id == query.Id).ToList()))
            {
                db.SRV_Clothe.DeleteObject(query);
            }

            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IQueryable<ReportStockData> SRV_CLOTHES_STOCKTACKINGSearch(DateTime date)
        {
            var query = from stock in db.SRV_CLOTHES_STOCKTACKING
                        where stock.Date_Time == date
                        select new ReportStockData
                        {
                            SizeId = stock.Size_Id,
                            Size_Code = stock.SRV_Clothe_Sizes.Size_Code.TrimEnd(),
                            Name = stock.SRV_Clothe_Sizes.SRV_Clothe.Name.TrimEnd(),
                            ClotheCode = stock.SRV_Clothe_Sizes.SRV_Clothe.Code.TrimEnd(),
                            SizeName = stock.SRV_Clothe_Sizes.Name.TrimEnd(),
                            SystemQty = stock.System_Qty,
                            Pysical_Qty = stock.Pysical_Qty,
                            Differance = stock.Pysical_Qty - stock.System_Qty,
                            Equating_Date_Time = stock.Equating_Date_Time != null ? (DateTime)stock.Equating_Date_Time : (DateTime?)null
                        };
            return query;

        }
        public static IEnumerable<object> getAllGrd(long campId, long CerId, long StageId, long GradeId)
        {
            var query =
                from list in db.SRV_Clothe
                where list.Campus_Id == campId && list.Cer_Id == CerId && list.Stage_Id == StageId && list.Grad_Id == GradeId
                select new
                     {
                         Id = list.Id,
                         Name = list.Name,
                         CampusName = list.ORG_Campus.Name,
                         CertificateName = list.ORG_Certificate != null ? (string)list.ORG_Certificate.Name : "",
                         StageName = list.ORG_Stage != null ? (string)list.ORG_Stage.Name : "",
                         GradeName = list.ORG_Grade != null ? (string)list.ORG_Grade.Name : "",
                         Code = list.Code,
                     };
            return query;
        }

        public static IEnumerable<OrderPayPrintData> PrintOrder(string stu_Name, string CamName, string CerName,
            string StageName, string GradeName, long PayNum, decimal Price)
        {
            List<OrderPayPrintData> lst = new List<OrderPayPrintData>();
            lst.Add(new OrderPayPrintData()
            {
                StudentName = stu_Name,
                CamName = CamName,
                CerName = CerName,
                StageName = StageName,
                GradeName = GradeName,
                PayNum = PayNum,
                Price = Price
            });
            return lst;
        }
     
        public static IEnumerable<GrdObjectClothe> PrintOrderDataGrd(IEnumerable<GrdObjectClothe> GrdLst)
        {
            return GrdLst;
        }
     
        public static IEnumerable<DiscountObject> PrintOrderDiscount(IEnumerable<DiscountObject> DiscountLst)
        {
            return DiscountLst;
        }
    }
    public class GrdObjectClothe
    {
        public int campusId;
        public int CampusId
        {
            get
            {
                return campusId;
            }
            set
            {
                campusId = value;
            }
        }

        public int studentId;
        public int StudentId
        {
            get
            {
                return studentId;
            }
            set
            {
                studentId = value;
            }
        }

        private string campusName;
        public string CampusName
        {
            get
            {
                return campusName;
            }
            set
            {
                campusName = value;
            }
        }

        public int cerId;
        public int CerId
        {
            get
            {
                return cerId;
            }
            set
            {
                cerId = value;
            }
        }

        private string cerName;
        public string CerName
        {
            get
            {
                return cerName;
            }
            set
            {
                cerName = value;
            }
        }

        public int stageId;
        public int StageId
        {
            get
            {
                return stageId;
            }
            set
            {
                stageId = value;
            }
        }

        private string stageName;
        public string StageName
        {
            get
            {
                return stageName;
            }
            set
            {
                stageName = value;
            }
        }

        public int gadeId;
        public int GradeId
        {
            get
            {
                return gadeId;
            }
            set
            {
                gadeId = value;
            }
        }

        private string gradeName;
        public string GradeName
        {
            get
            {
                return gradeName;
            }
            set
            {
                gradeName = value;
            }
        }
        public int clotheId;
        public int ClotheId
        {
            get
            {
                return clotheId;
            }
            set
            {
                clotheId = value;
            }
        }

        private string clotheName;
        public string ClotheName
        {
            get
            {
                return clotheName;
            }
            set
            {
                clotheName = value;
            }
        }

        public int sizeId;
        public int SizeId
        {
            get
            {
                return sizeId;
            }
            set
            {
                sizeId = value;
            }
        }

        private string sizeName;
        public string SizeName
        {
            get
            {
                return sizeName;
            }
            set
            {
                sizeName = value;
            }
        }

        public long priceId;
        public long PriceId
        {
            get
            {
                return priceId;
            }
            set
            {
                priceId = value;
            }
        }

        private double priceName;
        public double PriceName
        {
            get
            {
                return priceName;
            }
            set
            {
                priceName = value;
            }
        }

        public int userQty;
        public int UserQty
        {
            get
            {
                return userQty;
            }
            set
            {
                userQty = value;
            }
        }

        private double totalPrice;
        public double TotalPrice
        {
            get
            {
                return totalPrice;
            }
            set
            {
                totalPrice = value;
            }
        }

        private double discountPercent;
        public double DiscountPercent
        {
            get
            {
                return discountPercent;
            }
            set
            {
                discountPercent = value;
            }
        }

        //public int DiscountId;
        private double priceWzDiscount;
        public double PriceWzDiscount
        {
            get
            {
                return priceWzDiscount;
            }
            set
            {
                priceWzDiscount = value;
            }
        }
    }
    public  class ReportStockData
    {
        public DateTime? _Equating_Date_Time;
        public DateTime? Equating_Date_Time
        {
            get
            {
                return _Equating_Date_Time;
            }
            set
            {
                _Equating_Date_Time = value;
            }
        }

        public long _Differance;
        public long Differance
        {
            get
            {
                return _Differance;
            }
            set
            {
                _Differance = value;
            }
        }

        public long _Pysical_Qty;
        public long Pysical_Qty
        {
            get
            {
                return _Pysical_Qty;
            }
            set
            {
                _Pysical_Qty = value;
            }
        }

        public long _SystemQty;
        public long SystemQty
        {
            get
            {
                return _SystemQty;
            }
            set
            {
                _SystemQty = value;
            }
        }

        public string _SizeName;
        public string SizeName
        {
            get
            {
                return _SizeName;
            }
            set
            {
                _SizeName = value;
            }
        }

        public string _ClotheCode;
        public string ClotheCode
        {
            get
            {
                return _ClotheCode;
            }
            set
            {
                _ClotheCode = value;
            }
        }
      
        public string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        
        public string _Size_Code;
        public string Size_Code
        {
            get
            {
                return _Size_Code;
            }
            set
            {
                _Size_Code = value;
            }
        }

        public long _SizeId;
        public long SizeId
        {
            get
            {
                return _SizeId;
            }
            set
            {
                _SizeId = value;
            }
        }

    }

    public class OrderPayPrintData
    {
        public string StudentName { get; set; }
        public string CamName { get; set; }
        public string CerName { get; set; }
        public string StageName { get; set; }
        public string GradeName { get; set; }
        public long PayNum { get; set; }
        public decimal Price { get; set; }
    }
}
